package com.controller;

import com.po.*;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private LeadService leadService;
    @Autowired
    private UserService userService;
    @Autowired
    private SuggestService suggestService;

    @GetMapping("/index")
    public String index(Model model){
        int a_num=activityService.findAllByState(1).size();
        int l_num=userService.findAll(1).size();
        int u_num=userService.findAll(0).size();
        int s_num=suggestService.findAll().size();

        Quantity quantity=new Quantity(a_num,l_num,u_num,s_num);
        model.addAttribute("Quantity",quantity);
        return "/admin/index";
    }

    @GetMapping("/admin_lead")
    public String admin_lead(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.ASC) Pageable pageable,Model model){
        Page<Lead> p=leadService.findAllByT(pageable);
        model.addAttribute("page",p);
        Quantity quantity=new Quantity();
        quantity.setN_lead_num((int) p.getTotalElements());
        model.addAttribute("Quantity",quantity);
        return "/admin/admin_lead";
    }

    @GetMapping("/lead/{id}")
    public String lead(@PathVariable Long id, Model model) {
        model.addAttribute("Mes",leadService.findById(id));
        return "/admin/leads";
    }

    @PostMapping("/agree/{ids}")
    @ResponseBody
    public Msg agree_lead(@PathVariable String ids) {
        if(ids.contains("-")){
            List<Long> lead_ids = new ArrayList<>();
            String[] str_ids = ids.split("-");
            //组装id的集合
            for (String string : str_ids) {
                lead_ids.add(Long.valueOf(string));
            }
            for(Long id:lead_ids) {
                Lead lead=leadService.findById(id);
                User u=userService.findInfo(lead.getUser().getId());
                u.setState(1);
                userService.saveUser(u);
                lead.setState(1);
                leadService.saveLead(lead);
            }
        }else{
            Lead lead=leadService.findById(Long.valueOf(ids));
            User u=userService.findInfo(lead.getUser().getId());
            u.setState(1);
            userService.saveUser(u);
            lead.setState(1);
            leadService.saveLead(lead);
        }
        return Msg.success();
    }

    @PostMapping("/refuse/{ids}")
    @ResponseBody
    public Msg refuse_lead(@PathVariable String ids) {
        if(ids.contains("-")){
            List<Long> lead_ids = new ArrayList<>();
            String[] str_ids = ids.split("-");
            //组装id的集合
            for (String string : str_ids) {
                lead_ids.add(Long.valueOf(string));
            }
            for(Long id:lead_ids) {
                Lead lead=leadService.findById(id);
                User u=userService.findInfo(lead.getUser().getId());
                u.setState(2);
                userService.saveUser(u);
                lead.setState(2);
                leadService.saveLead(lead);
            }
        }else{
            Lead lead=leadService.findById(Long.valueOf(ids));
            User u=userService.findInfo(lead.getUser().getId());
            u.setState(2);
            userService.saveUser(u);
            lead.setState(2);
            leadService.saveLead(lead);
        }
        return Msg.success();
    }

    @GetMapping("/admin_activity")
    public String admin_activity(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.ASC) Pageable pageable,Model model){
        Page<Activity> p=activityService.findAllByT(pageable);
        model.addAttribute("page1",p);
        Quantity quantity=new Quantity();
        quantity.setN_activity_num((int) p.getTotalElements());
        model.addAttribute("Quantity",quantity);
        return "/admin/admin_activity";
    }

    @GetMapping("/activity/{id}")
    public String activity(@PathVariable Long id, Model model) {
        System.out.println("id="+id);
        model.addAttribute("Mes1",activityService.findById(id));
        System.out.println(model.getAttribute("Mes1"));
        return "/admin/activities";
    }

    @PostMapping("/agree1/{ids}")//admin/activity/agree/2
    @ResponseBody
    public Msg agree_act(@PathVariable String ids) {
        System.out.println("ids="+ids);
        if(ids.contains("-")){
            List<Long> activity_ids = new ArrayList<>();
            String[] str_ids = ids.split("-");
            //组装id的集合
            for (String string : str_ids) {
                activity_ids.add(Long.valueOf(string));
            }
            for(Long id:activity_ids) {
                Activity a=activityService.findById(id);
                a.setState(1);
                activityService.saveActivity(a);
            }
        }else{
            System.out.println("222");
            Activity a=activityService.findById(Long.valueOf(ids));
            a.setState(1);
            activityService.saveActivity(a);
            System.out.println("333");
        }
        return Msg.success();
    }

    @PostMapping("/refuse1/{ids}")
    @ResponseBody
    public Msg refuse_act(@PathVariable String ids) {
        if(ids.contains("-")){
            List<Long> activity_ids = new ArrayList<>();
            String[] str_ids = ids.split("-");
            //组装id的集合
            for (String string : str_ids) {
                activity_ids.add(Long.valueOf(string));
            }
            for(Long id:activity_ids) {
                Activity a=activityService.findById(id);
                a.setState(2);
                activityService.saveActivity(a);
            }
        }else{
            Activity a=activityService.findById(Long.valueOf(ids));
            a.setState(2);
            activityService.saveActivity(a);
        }
        return Msg.success();
    }

    @GetMapping("/suggest")
    public String suggest(Model model){
        List<Suggest> suggests=suggestService.findAll();
        Quantity quantity=new Quantity();
        quantity.setSuggest_num(suggests.size());
        model.addAttribute("Suggest",suggests);
        model.addAttribute("Quantity",quantity);
        return "/admin/suggest";
    }

//    @GetMapping("/manage_user")
//    public String manage_user(Model model){
//        int u_num=userService.findAll(0).size();
//        Quantity quantity=new Quantity();
//        quantity.setUser_num(u_num);
//        model.addAttribute("Quantity",quantity);
//        return "/admin/manage_user";
//    }

//    @GetMapping("/manage_lead")
//    public String manage_lead(Model model){
//        List<Lead> l=leadService.findAll(1);
//        int l_num=l.size();
//        Quantity quantity=new Quantity();
//        quantity.setLead_num(l_num);
//        model.addAttribute("Quantity",quantity);
//        model.addAttribute("Leads",l);
//        return "/admin/manage_lead";
//    }
}
